import Vue from 'vue'
import VueRouter from 'vue-router';
import LoginView from '@/views/login/LoginView.vue'
import RegisterView from '@/views/login/RegisterView.vue'
import ForgetPwdView from "@/views/login/ForgetPwdView.vue";
import ResetPwdView from "@/views/login/ResetPwdView.vue";
import AdminView from "@/views/admin/AdminView.vue";
import UserView from "@/views/user/UserView.vue";
import BusinessView from "@/views/business/BusinessView.vue";

import HomeView from '@/views/user/HomeView.vue';
import LogView from "@/views/admin/LogView.vue";
import DetailView from "@/views/user/DetailView.vue";
import BusinessTreeInfoView from "@/views/business/BusinessTreeInfoView.vue";
import BusinessHomeView from "@/views/business/BusinessHomeView.vue";
import UserInfoView from "@/views/admin/UserInfoView.vue";
import BusinessInfoView from "@/views/admin/BusinessInfoView.vue";
import TreeSelect from "@/views/user/TreeSelect.vue";
import ShopperService from "@/views/user/ShopperService.vue";
import UserSelfView from "@/views/user/UserSelfView.vue";
import SelfInfoView from "@/views/user/SelfInfoView.vue";
import SelfCollectView from "@/views/user/SelfCollectView.vue";
import SelfFeedbackView from "@/views/user/SelfFeedbackView.vue";
import SelfOrder from "@/views/user/SelfOrder.vue";
import BusinessShop from "@/views/business/BusinessShop.vue";
import RecommendUploadTree from "@/views/user/RecommendUploadTree.vue";
import BusinessSelfView from "@/views/business/BusinessSelfView.vue";
import Notice from "@/views/Notice.vue";
import BusinessSelfInfoView from "@/views/business/BusinessSelfInfoView.vue";
import AdminSelfInfoView from "@/views/admin/AdminSelfInfoView.vue";
import CheckApplyView from "@/views/admin/CheckApplyView.vue";
import RegisterShopperView from "@/views/login/RegisterShopperView.vue";
import AdminNotice from "@/views/admin/AdminNotice.vue";
import BusinessOrderView from "@/views/business/BusinessOrderView.vue";
import BusinessChangeOrderView from "@/views/business/BusinessChangeOrderView.vue";
import BusinessSelfCollectView from "@/views/business/BusinessSelfCollectView.vue";
import OtherTreeInfoView from "@/views/business/OtherTreeInfoView.vue";
import BusinessDetailView from "@/views/business/BusinessDetailView.vue";
import UserFeedbackInfoView from "@/views/business/UserFeedbackInfoView.vue";
import userFeedbackView from "@/views/admin/UserFeedbackView.vue";


Vue.use(VueRouter)

const routes = [
    //登录
    {
        path: '/loginview',
        name: 'loginview',
        component: LoginView
    },
    //注册用户
    {
        path: '/registerview',
        name: 'registerview',
        component: RegisterView
    },
    //注册商家
    {
        path: '/registershopperview',
        name: 'registershopperview',
        component: RegisterShopperView
    },
    //忘记密码
    {
        path: '/forgetpwdview',
        name: 'forgetpwdview',
        component: ForgetPwdView
    },
    //重置密码
    {
        path: '/resetpwdview',
        name: 'resetpwdview',
        component: ResetPwdView
    },
    //管理员界面
    {
        path: '/adminview',
        name: 'adminview',
        component: AdminView,
        redirect: '/checkapplyview',
        children: [
            //显示用户信息
            {
                path: '/userinfoview',
                name: 'userinfoview',
                component: UserInfoView
            },
            //显示商家信息
            {
                path: '/businessinfoview',
                name: 'businessinfoview',
                component: BusinessInfoView
            },
            //管理系统日志
            {
                path: '/adiminlogview',
                name: 'adiminlogview',
                component: LogView
            },
            //用户反馈
            {
                path: '/userfeedbackview',
                name: 'userfeedbackview',
                component: userFeedbackView
            },
            //管理员个人信息
            {
                path: '/adminselfinfoview',
                name: 'adminselfinfoview',
                component: AdminSelfInfoView
            },
            //管理系统公告
            {
                path: '/adminnoticeview',
                name: 'adminnoticeview',
                component: AdminNotice
            },
            //审核申请
            {
                path: '/checkapplyview',
                name: 'checkapplyview',
                component: CheckApplyView
            }
        ]
    },
    //商家界面
    {
        path: '/businessview',
        name: 'businessview',
        component: BusinessView,
        redirect: '/businesshomeview',
        children: [
            //商家首页
            {
                path: '/businesshomeview',
                name: 'businesshomeview',
                component: BusinessHomeView
            },
            //其余商家树苗信息
            {
                path: '/othertreeinfoview',
                name: 'othertreeinfoview',
                component: OtherTreeInfoView,
            },
            //商品详情
            {
                path: '/businessdetailview',
                name: 'businessdetailview',
                component: BusinessDetailView
            },
            //用户反馈信息
            {
                path: '/userfeedbackinfoview',
                name: 'userfeedbackinfoview',
                component: UserFeedbackInfoView,
            },
            //商家个人中心
            {
                path: '/businessselfview',
                name: 'businessselfview',
                component: BusinessSelfView,
                redirect: '/businessselfinfoview',
                children: [
                    //商家个人信息
                    {
                        path: '/businessselfinfoview',
                        name: 'businessselfinfoview',
                        component: BusinessSelfInfoView
                    },
                    //我的店铺
                    {
                        path: '/businessshop',
                        name: 'businessshop',
                        component: BusinessShop,
                    },
                    //我的树苗
                    {
                        path: '/businesstreeinfoview',
                        name: 'businesstreeinfoview',
                        component: BusinessTreeInfoView,
                    },
                    //商家我的收藏
                    {
                        path: '/businessselfcollectview',
                        name: 'businessselfcollectview',
                        component: BusinessSelfCollectView
                    },
                    //用户订单信息
                    {
                        path: '/businessorderview',
                        name: 'businessorderview',
                        component: BusinessOrderView
                    },
                    //商家交换订单信息
                    {
                        path: '/businesschangeorderview',
                        name: 'businesschangeorderview',
                        component: BusinessChangeOrderView
                    },
                ]
            },
            //查看系统公告
            {
                path: '/businessnotice',
                name: 'businessnotice',
                component: Notice
            },

        ]
    },
    //用户界面
    {
        path: '/userview',
        name: 'userview',
        component: UserView,
        redirect: '/homeview',
        children: [
            //用户系统首页
            {
                path: '/homeview',
                name: 'homeview',
                component: HomeView
            },
            //树苗精选
            {
                path: '/treeselect',
                name: 'treeselect',
                component: TreeSelect
            },
            //商品详情
            {
                path: '/detailview',
                name: 'detailview',
                component: DetailView
            },
            //个人中心
            {
                path: '/userselfview',
                name: 'userselfview',
                component: UserSelfView,
                redirect: "/selfinfoview",
                children: [
                    //我的信息
                    {
                        path: '/selfinfoview',
                        name: 'selfinfoview',
                        component: SelfInfoView,
                    },
                    //我的收藏
                    {
                        path: '/selfcollectview',
                        name: 'selfcollectview',
                        component: SelfCollectView,
                    },
                    //我的反馈
                    {
                        path: '/selffeedbackview',
                        name: 'selffeedbackview',
                        component: SelfFeedbackView,
                    },
                    //我的订单
                    {
                        path: '/selforder',
                        name: 'selforder',
                        component: SelfOrder,
                    },
                    //商家服务评分
                    {
                        path: '/shopperservice',
                        name: 'shopperservice',
                        component: ShopperService,
                    },
                    //推荐商家上架
                    {
                        path: '/recommenduploadtree',
                        name: 'recommenduploadtree',
                        component: RecommendUploadTree,
                    },
                ]
            },
            //联系我们
            {
                path: '/phone',
                name: 'phone',
                component: () => import('@/views/Phone.vue')
            },
            //系统公告
            {
                path: '/usernotice',
                name: 'usernotice',
                component: () => import('@/views/Notice.vue')
            },
            //系统日志
            {
              path: '/logview',
              name: 'logview',
              component: LogView
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

//路由守卫
router.beforeEach((to, from, next) => {

    const user = sessionStorage.getItem("user");
    if (!user) {
        if (to.path === '/loginview') {
            next();
        } else if (to.path === '/registerview') {
            next();
        } else if (to.path === '/registershopperview') {
            next();
        } else if (to.path === '/forgetpwdview') {
            next();
        } else if (to.path === '/resetpwdview') {
            next();
        } else {
            next('/loginview');
        }
    }
    next();

})

export default router
